//
//  AgentIntroduceViewController.m
//  BaseProject
//
//  Created by 王洋 on 2021/3/2.
//  Copyright © 2021 王洋. All rights reserved.
//
#import "CommonMediaModel.h"
#import "DetailImageTableViewCell.h"

#import "AgentApplyViewController.h"

#import "AgentIntroduceViewController.h"

@interface AgentIntroduceViewController ()<UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>

@property (nonatomic, strong)UIScrollView *tableview;

@property (nonatomic, strong)UIButton *bottomBtn;

@property (nonatomic, strong) NSMutableArray <CommonMediaModel *>*dataArray;

@end

@implementation AgentIntroduceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self prepareData];

}

- (void)prepareData {
    
    NSMutableArray *array = [NSMutableArray array];
    for (NSInteger index = 0; index < 3; index ++) {
        NSString *url = @"";
        NSString *localUrl = @"";
        CGFloat itemHeight = 100;
        if (index == 0) {
            url = AgentIntroduceImageUrl1;
            localUrl = @"Recommender1";
        } else if (index == 1)  {
            url = AgentIntroduceImageUrl2;
            localUrl = @"Recommender2";
        } else if (index == 2)  {
            url = AgentIntroduceImageUrl3;
            localUrl = @"Recommender3";
        }
        [array addObject:@{
            @"url": url,
            @"localUrl": localUrl,
            @"itemHeight": @(itemHeight)
        }];
    }
    
    self.dataArray = @[].mutableCopy;
    CGFloat orinY = 0;
    for (NSInteger index = 0; index < 3; index ++) {
        NSDictionary *dic = array[index];
        CommonMediaModel *model = [CommonMediaModel modelWithDictionary:dic];
        [self.dataArray addObject:model];
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, orinY, kScreenWidth, 100)];
        imageView.image = [UIImage imageNamed:model.localUrl];
        imageView.height = imageView.image.size.height * kScreenWidth / imageView.image.size.width;
        orinY = imageView.bottom;
        if (index == 0) {
            orinY = imageView.height * 0.32;
        }
        if (index == 1) {
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(applyAgent)];
            imageView.userInteractionEnabled = YES;
            [imageView addGestureRecognizer:tap];
        }
        
        [self.tableview addSubview:imageView];
    }
    self.tableview.contentSize = CGSizeMake(0, orinY + 100);

}

- (void)initView {

    //屏幕常亮
    [UIApplication sharedApplication].idleTimerDisabled = YES;
//    self.navigationController.navigationBarHidden = YES;
//    #if RT_INTERACTIVE_PUSH
//        self.rt_navigationController.rt_enableInteractivePush = YES;
//    #endif
    
    self.title = @"用户推荐官";
    
    if([self.tableview respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
        if (@available(iOS 11.0, *)) {
            self.tableview.contentInsetAdjustmentBehavior=UIScrollViewContentInsetAdjustmentNever;
        } else {
            // Fallback on earlier versions
        }
    }
    
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    
    [self.view addSubview:self.bottomBtn];
    self.bottomBtn.hidden = YES;

    CGFloat height = (TTScreenWidth/1077)*216;
    [self.bottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(4);
        make.right.mas_equalTo(-4);
        make.height.mas_equalTo(height);
        
        CGFloat bottom = 0;
        if (Bottom_SafeHeight > 0) {
            bottom = - Bottom_SafeHeight + 20;
        }
        make.bottom.mas_equalTo(bottom);
    }];
}

- (void)applyAgent {
    AgentApplyViewController *vc = [[AgentApplyViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark UITableviewDelegate


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat scrollHeight = self.dataArray[0].itemHeight + self.dataArray[1].itemHeight;
    NSLog(@"%lf %lf", scrollHeight, scrollView.contentOffset.y);
    if (scrollView.contentOffset.y>scrollHeight) {
        self.bottomBtn.hidden = NO;
    } else {
        self.bottomBtn.hidden = YES;
    }
}

- (UIScrollView *)tableview {
    if (!_tableview) {
        UIScrollView *view = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kTopHeight)];
        [self.view addSubview:view];
        if (@available(iOS 11.0, *)) {
            view.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        view.backgroundColor = kBGDarkColor;
        view.delegate = self;
        _tableview = view;
    }
    return _tableview;
}

- (UIButton *)bottomBtn {
    if (!_bottomBtn) {
        _bottomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_bottomBtn setImage:[UIImage imageNamed:@"agent_apply"] forState:UIControlStateNormal];
        [_bottomBtn addTarget:self action:@selector(applyAgent) forControlEvents:UIControlEventTouchUpInside];
    }
    return _bottomBtn;
}

@end
